package com.authine.cloudpivot.ext.entity.order.extendsAttr;

import cn.hutool.core.lang.Console;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.io.Serializable;

/**
 * 客户订单扩展字段
 *
 * @author 张天赐
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderCustomerExtendsAttr implements Serializable {

    private static final long serialVersionUID = 4910301174278321270L;
    /**
     * 下单定制化流程审批意见
     */
    private String approvalOpinion;
    /**
     * 当前审批人
     */
    private String approvalBy;
    /**
     * 商城下单时选择的个人开票信息id
     */
    private Long memberInvoiceId;
    /**
     * 商城下单时选择的收票地址id
     */
    private Long invoiceAddressId;
    /**
     * 登录人名称
     */
    private String username;
    /**
     * OS订单号
     */
    private String clientOs;
    /**
     * 地址
     */
    private String address;
    /**
     * 联系人
     */
    private String receiver;
    /**
     * 联系电话
     */
    private String phone;

    public static OrderCustomerExtendsAttr conversion(String value, boolean ifThrow) {
        OrderCustomerExtendsAttr result = OrderCustomerExtendsAttr.builder().build();
        ObjectMapper objectMapper = DateUtils.objectMapper();

        try {
            if (RegexUtils.notNull(value)) {
                result = objectMapper.readValue(value, OrderCustomerExtendsAttr.class);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("订单扩展属性字符串转换对象失败");
            }
        }

        return result;
    }

    public static String conversion(OrderCustomerExtendsAttr value, boolean ifThrow) {
        String result = "";
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            if (null != value) {
                result = objectMapper.writeValueAsString(value);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("订单扩展属性对象转换字符串失败");
            }
        }

        return result;
    }
}
